package npcSystem.mainCharSystem;

import java.awt.image.BufferedImage;
import java.io.*;

import tools.Constants;
import tools.ImageHandler;

public abstract class WeaponGetter {

	public static Weapon getWeapon(int id)
	{
		Weapon weapon = null;
		id = id - 4;
		
		InputStream iS = WeaponGetter.class.getResourceAsStream("/npcSystem/mainCharSystem/weaponList");
		if(iS == null) return null;
		InputStreamReader iR = new InputStreamReader(iS);
		BufferedReader reader = new BufferedReader(iR);
		
		String curLine = null;
		
		try {curLine = reader.readLine();} 
		catch (IOException e) {e.printStackTrace();}
		
		int code = -1;
		double pwr = 0;
		int imgTag = 0;
		BufferedImage image = null;
				
		while(curLine != null)
		{
			if(curLine.contains("weapons"))
			{
				while(curLine != null && !curLine.contains("}"))
				{
					try {curLine = reader.readLine();} 
					catch (IOException e) {e.printStackTrace();}
					if(!curLine.contains("{") && !curLine.contains("}") && curLine != null)
					{
						code = Integer.parseInt(curLine.split("<")[0]);
						if(code == id)
						{
							curLine = curLine.split("<")[1];
							pwr = Double.parseDouble(curLine.split(":")[0]);
							
							imgTag = Integer.parseInt(curLine.split(":")[1])-1;
							image = ImageHandler.loadImage("/img/mainChar/weapons.png");
							image = image.getSubimage(0, imgTag*8*Constants.WEAPON_SPRITE_HEIGHT, 
									image.getWidth(), 8*Constants.WEAPON_SPRITE_HEIGHT);
							image = ImageHandler.addColorKey(image);
							
							weapon = new Weapon(pwr, image);
						}
					}
				}
			}
			try {curLine = reader.readLine();} 
			catch (IOException e) {e.printStackTrace();}
		}
		
		return weapon;
	}

}
